package system

import (
	"github.com/goravel/framework/contracts/http"
	"github.com/goravel/framework/facades"
	httpfacade "github.com/hulutech-web/http_result"
	"goravel/app/http/requests"
	"goravel/app/models/systemEntity"
)

type PermissionController struct {
	//Dependent services
}

func NewPermissionController() *PermissionController {
	return &PermissionController{
		//Inject services
	}
}

// Index 分页查询，支持搜索，路由参数?name=xxx&pageSize=1&currentPage=1&sort=xxx&order=xxx,等其他任意的查询参数
// @Summary      分页查询
// @Description  分页查询
// @Tags         PermissionController
// @Accept       json
// @Produce      json
// @Id PermissionIndex
// @Security ApiKeyAuth
// @Param Authorization header string false "Bearer 用户令牌"
// @Param  name  query  string  false  "name"
// @Param  pageSize  query  string  false  "pageSize"
// @Param  currentPage  query  string  false  "currentPage"
// @Param  sort  query  string  false  "sort"
// @Param  order  query  string  false  "order"
// @Success 200 {string} json {}
// @Router       /api/permission [get]
func (r *PermissionController) Index(ctx http.Context) http.Response {
	Permissions := []systemEntity.Permission{}
	queries := ctx.Request().Queries()
	res, err := httpfacade.NewResult(ctx).SearchByParams(queries, nil).ResultPagination(&Permissions)
	if err != nil {
		return httpfacade.NewResult(ctx).Error(http.StatusInternalServerError, "", err.Error())
	}
	return res
}

// List 列表查询
// @Summary      列表查询
// @Description  列表查询
// @Tags         PermissionController
// @Accept       json
// @Produce      json
// @Id PermissionList
// @Security ApiKeyAuth
// @Param Authorization header string false "Bearer 用户令牌"
// @Success 200 {string} json {}
// @Router       /api/permission/list [get]
func (r *PermissionController) List(ctx http.Context) http.Response {
	Permissions := []systemEntity.Permission{}
	queries := ctx.Request().Queries()
	httpfacade.NewResult(ctx).SearchByParams(queries, nil).List(&Permissions)
	return httpfacade.NewResult(ctx).Success("", Permissions)
}

// Show 查询
// @Summary      查询
// @Description  查询
// @Tags         PermissionController
// @Accept       json
// @Produce      json
// @Id PermissionShow
// @Security ApiKeyAuth
// @Param Authorization header string false "Bearer 用户令牌"
// @Param id path int true "id"
// @Success 200 {string} json {}
// @Router       /api/permission/{id} [get]
func (r *PermissionController) Show(ctx http.Context) http.Response {
	id := ctx.Request().RouteInt("id")
	Permission := systemEntity.Permission{}
	facades.Orm().Query().Model(&systemEntity.Permission{}).Where("id = ?", id).First(&Permission)
	return httpfacade.NewResult(ctx).Success("", Permission)
}

// Store 新增
// @Summary      新增
// @Description  新增
// @Tags         PermissionController
// @Accept       json
// @Produce      json
// @Id PermissionStore
// @Security ApiKeyAuth
// @Param Authorization header string false "Bearer 用户令牌"
// @Param PermissionData body requests.PermissionRequest true "数据"
// @Success 200 {string} json {}
// @Router       /api/admin/permission [post]
func (r *PermissionController) Store(ctx http.Context) http.Response {
	var PermissionRequest requests.PermissionRequest
	errors, err := ctx.Request().ValidateRequest(&PermissionRequest)
	if err != nil {
		return httpfacade.NewResult(ctx).Error(http.StatusInternalServerError, "数据错误", err.Error())
	}
	if errors != nil {
		return httpfacade.NewResult(ctx).ValidError("", errors.All())
	}
	Permission := systemEntity.Permission{}
	//todo add request values
	//SystemUserLevel = systemEntity.SystemUserLevel{
	//	Name:      SystemUserLevelRequest.Name,
	//	Money:     SystemUserLevelRequest.Money,
	//	Discount:  SystemUserLevelRequest.Discount,
	//	IsPay:     SystemUserLevelRequest.IsPay,
	//	IsForever: SystemUserLevelRequest.IsForever,
	//	Grade:     SystemUserLevelRequest.Grade,
	//	Image:     SystemUserLevelRequest.Image,
	//	Icon:      SystemUserLevelRequest.Icon,
	//	Explain:   SystemUserLevelRequest.Explain,
	//	ExpNum:    SystemUserLevelRequest.ExpNum,
	//}
	facades.Orm().Query().Model(&systemEntity.Permission{}).Create(&Permission)
	return httpfacade.NewResult(ctx).Success("创建成功", nil)
}

// Update
// @Summary      更新
// @Description  更新
// @Tags         PermissionController
// @Accept       json
// @Produce      json
// @Id PermissionUpdate
// @Security ApiKeyAuth
// @Param Authorization header string false "Bearer 用户令牌"
// @Param PermissionData body requests.PermissionRequest true "数据"
// @Success 200 {string} json {}
// @Router       /api/admin/permission/{id} [put]
func (r *PermissionController) Update(ctx http.Context) http.Response {
	id := ctx.Request().Route("id")
	Permission := systemEntity.Permission{}
	facades.Orm().Query().Model(&systemEntity.Permission{}).Where("id=?", id).Find(&Permission)
	if Permission.ID == 0 {
		return httpfacade.NewResult(ctx).Error(http.StatusInternalServerError, "数据不存在", "")
	}
	var PermissionRequest requests.PermissionRequest
	errors, err := ctx.Request().ValidateRequest(&PermissionRequest)
	if err != nil {
		return httpfacade.NewResult(ctx).Error(http.StatusInternalServerError, "数据错误", err.Error())
	}
	if errors != nil {
		return httpfacade.NewResult(ctx).ValidError("", errors.All())
	}
	//todo add request values
	//SystemUserLevel = systemEntity.SystemUserLevel{
	//	Name:      SystemUserLevelRequest.Name,
	//	Money:     SystemUserLevelRequest.Money,
	//	Discount:  SystemUserLevelRequest.Discount,
	//	IsPay:     SystemUserLevelRequest.IsPay,
	//	IsForever: SystemUserLevelRequest.IsForever,
	//	Grade:     SystemUserLevelRequest.Grade,
	//	Image:     SystemUserLevelRequest.Image,
	//	Icon:      SystemUserLevelRequest.Icon,
	//	Explain:   SystemUserLevelRequest.Explain,
	//	ExpNum:    SystemUserLevelRequest.ExpNum,
	//}
	facades.Orm().Query().Model(&systemEntity.Permission{}).Where("id=?", id).Update(&Permission)
	return httpfacade.NewResult(ctx).Success("修改成功", nil)
}

// Destroy 删除
// @Summary      删除
// @Description  删除
// @Tags         PermissionController
// @Accept       json
// @Produce      json
// @Id PermissionDestroy
// @Security ApiKeyAuth
// @Success 200 {string} json {}
// @Router       /api/admin/permission/{id} [delete]
func (r *PermissionController) Destroy(ctx http.Context) http.Response {
	id := ctx.Request().Route("id")
	facades.Orm().Query().Model(&systemEntity.Permission{}).Where("id=?", id).Delete(&systemEntity.Permission{})
	return httpfacade.NewResult(ctx).Success("删除成功", nil)
}

// Option 选项
// @Summary      选项
// @Description  选项
// @Tags         PermissionController
// @Accept       json
// @Produce      json
// @Id PermissionOption
// @Security ApiKeyAuth
// @Success 200 {string} json {}
// @Router       /api/admin/permission/option [get]
func (r *PermissionController) Option(ctx http.Context) http.Response {
	Permissions := []systemEntity.Permission{}
	queries := ctx.Request().Queries()
	res, _ := httpfacade.NewResult(ctx).SearchByParams(queries, nil).List(&Permissions)
	return res
}
